//
//  CatchErrorViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/25.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxSwift

class CatchErrorViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addTapWithHandle { (ta) in
            
            enum TestError: Error {
                case test
            }
            
            let bag = DisposeBag()
            
            let sequence = PublishSubject<Int>()
            let recovery = PublishSubject<Int>()
            
            sequence.catchError({ (error) -> Observable<Int> in
                print(error)
                return recovery
            }).subscribe({ (eve) in
                print(eve)
            }).disposed(by: bag)
            
            sequence.onNext(1)
            sequence.onNext(2)
            
            // 不发生异常的话 下面的recovery的onNext 不会调用
            sequence.onError(TestError.test)
            
            recovery.onNext(21)
            recovery.onNext(22)
            recovery.onNext(23)
        }
    }
}
